package com.rabbit.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by IntelliJ IDEA.
 * <p>
 * Author: libingyang
 * Date: 2021-03-15 15:35
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("192.168.100.54");

        factory.setPort(5672);

        factory.setVirtualHost("/");

        factory.setUsername("guest");

        factory.setPassword("guest");

        // 获取连接对象
        Connection connection = factory.newConnection();

        // 获取连接通道
        Channel channel = connection.createChannel();

        // 通道绑定消息队列
        /**
         * 参数
         * 1 队列名称，队列不存在 自动创建
         * 2 用来定义队列是否要持久化
         * 3 是否是独占队列
         * 4 是否在消费完成删除队列
         * 5 额外附加参数
         */
        channel.queueDeclare("task_queue", true, false, false, null);


        String message = "世界你好！";

        // 发送消息
        /**
         * 1 交换机名称
         * 2 队列名称
         * 3 传递消息的额外设置
         * 4 具体的消息内容
         */
        for(int i=0;i<=20;i++) {

            channel.basicPublish("", "task_queue", null, (i + message).getBytes());

        }

        channel.close();
        connection.close();
    }
}
